-- NewBagUpView

local IconRender = require("app.widget.renders.IconRender")
local Item = require("app.public.dao.Item")

local itemDistanceOfX = 0 -- 消耗品X间距
local itemMiddlePosX = 0 -- 消耗品居中X坐标
local itemWidth = 0 -- 消耗品item宽度

local OPEN_TAG = 1
local CLOSE_TAG = 2
local PLAYING_TAG = 3

local NewBagUpView = class("NewBagUpView", app.mvc.ViewBase)
local Armature = require("app.public.avatar.Armature")
NewBagUpView.VIEW_ROOT_RES = Res.NewBagUpUi

function NewBagUpView:init(data)
	self.super.init(self, data)
	display.setCascadeOpacityEnabled(self._ui['Panel'], true)

	self._ui.bg:setTouchEnabled(true)
	display.uiAddClick(self._ui.bg, function()
        self:closeView()
    end)
	
	itemDistanceOfX = self._ui["Panel/Panel_content/Panel_item/item_2"]:getPositionX() - self._ui["Panel/Panel_content/Panel_item/item_1"]:getPositionX()
	itemMiddlePosX = self._ui["Panel/Panel_content/Panel_item/item_3"]:getPositionX()
	itemWidth = self._ui["Panel/Panel_content/Panel_item/item_3"]:getContentSize().width

	self._ui["Panel/Panel_content/Button_cancel"]:addClickEventListener(function()
			self:closeView()
		end)
	self._ui["Panel/Panel_content/Button_up"]:addClickEventListener(function()
			executeFunc(self._onOperate, "BagUpPanel_extendBag")
			self:closeView()
		end)

	self._curBagName = self._ui["Panel/Panel_content/Panel_data/name_1"]
	self._curBagNum = self._ui["Panel/Panel_content/Panel_data/num_1"]
	self._nextBagName = self._ui["Panel/Panel_content/Panel_data/name_2"]
	self._nextBagNum = self._ui["Panel/Panel_content/Panel_data/num_2"]

	local goldPanel = self._ui["Panel/Panel_content/Panel_gold"]
	self._goldPanel = goldPanel
	self._lvTitle = goldPanel:getChildByName("lv_tlt")
	self._lvNeeded = goldPanel:getChildByName("lv_num")
	self._goldTitle = goldPanel:getChildByName("gold_tlt")
	self._gold_icon = goldPanel:getChildByName("gold_icon")
	self._goldNeeded = goldPanel:getChildByName("gold_num")

end

function NewBagUpView:setData(data)
	self._data = data
	self:refresh()
end

-- 填充升级背包所需内容
function NewBagUpView:refresh() -- self._data = bagInfo
	self._curBagName:setString( L(self._data.curConf.name) )
	self._curBagNum:setString( self._data.maxNum )

	self._nextBagName:setString( L(self._data.nextConf.name) )
	self._nextBagNum:setString( tonumber(self._curBagNum:getString())  + tonumber(self._data.nextConf.cellNum) - tonumber(self._data.curConf.cellNum) )

	-- 需求等级
	local lv = tonumber(self._data.curConf.needLv)
	if lv then
		self._lvTitle:setVisible( true )
		self._lvNeeded:setVisible( true )

		self._lvNeeded:setString( lv )
		if lv > UD:getLevel() then
			self._lvNeeded:setColor( display.COLOR_RED )
		else
			self._lvNeeded:setColor( display.COLOR_WHITE )
		end
	else
		self._lvTitle:setVisible( false )
		self._lvNeeded:setVisible( false )
	end

	-- 需求物品
	for i = 1, 5 do
		self._ui["Panel/Panel_content/Panel_item/item_"..i]:setVisible( false )
	end
	if self._data.curConf.needItem and "" ~= self._data.curConf.needItem then -- 有消耗？
		local costItemList = UD:parseItemsFromString( self._data.curConf.needItem )
		local costItemNum = #costItemList -- 消耗的物品个数

		for i,v in ipairs(costItemList) do
			local nodeTemp = IconRender.create( self._ui["Panel/Panel_content/Panel_item/item_"..i] )

			nodeTemp:setVisible( true )
			nodeTemp:setData( v )
			nodeTemp:setTipEnable( true )
			nodeTemp:setEquiped( false )

			nodeTemp:setTipsCloseCallBack(handler(self, self._onTipClose))

			local numPanel = nodeTemp:getChildByName( "Panel" ):getChildByName( "Panel_num" )
			numPanel:setVisible( true )
			local numCount = numPanel:getChildByName( "num" )
			local hasNum = UD:getNumByRenderData( v )
			numCount:setString( string.format( "%d", v.amount ) )
			if v.amount > hasNum then --道具足?
				numCount:setColor( display.COLOR_RED )
			else
				numCount:setColor( display.COLOR_WHITE )
			end
		end

		-- 以下条件语句模块：调整消耗品居中
		if 0 == costItemNum%2 then -- 偶数个消耗
			local leftMid = costItemNum/2 -- 左侧标记item
			local leftMidPosX = itemMiddlePosX - ( itemWidth + itemDistanceOfX ) / 2 -- 左侧标记item坐标X
			self._ui["Panel/Panel_content/Panel_item/item_"..leftMid]:setPositionX( leftMidPosX )
			for i = leftMid - 1, 1, -1 do
				local posTemp = leftMidPosX - ( itemWidth + itemDistanceOfX ) * ( leftMid - i )
				self._ui["Panel/Panel_content/Panel_item/item_"..i]:setPositionX( posTemp )
			end

			local rightMid = costItemNum/2 + 1 -- 右侧标记item
			local rightMidPosX = itemMiddlePosX + ( itemWidth + itemDistanceOfX ) / 2 -- 右侧标记item坐标X
			self._ui["Panel/Panel_content/Panel_item/item_"..rightMid]:setPositionX( rightMidPosX )
			for i = rightMid + 1, costItemNum do
				local posTemp = rightMidPosX + ( itemWidth + itemDistanceOfX ) * ( i - rightMid )
				self._ui["Panel/Panel_content/Panel_item/item_"..i]:setPositionX( posTemp )
			end
		else
			local midNum = ( costItemNum + 1 ) / 2
			self._ui["Panel/Panel_content/Panel_item/item_"..midNum]:setPositionX( itemMiddlePosX )
			for i = midNum - 1, 1, -1 do
				local posTemp = itemMiddlePosX - ( itemWidth + itemDistanceOfX ) * ( midNum - i )
				self._ui["Panel/Panel_content/Panel_item/item_"..i]:setPositionX( posTemp )
			end
			for i = midNum + 1, costItemNum do
				local posTemp = itemMiddlePosX + ( itemWidth + itemDistanceOfX ) * ( i - midNum )
				self._ui["Panel/Panel_content/Panel_item/item_"..i]:setPositionX( posTemp )
			end
		end
	end

	local goldNeed = tonumber( self._data.curConf.needGold )
	if goldNeed > 0 then -- 有消耗？
		self._goldTitle:setVisible( true )
		self._gold_icon:setVisible( true )
		self._goldNeeded:setVisible( true )

		self._goldNeeded:setString( goldNeed )

		if goldNeed <= UD:getGold() then
			self._goldNeeded:setColor( display.COLOR_WHITE )
		else
			self._goldNeeded:setColor( display.COLOR_RED )
		end
	else
		self._goldTitle:setVisible( false )
		self._gold_icon:setVisible( false )
		self._goldNeeded:setVisible( false )
	end

	if lv and goldNeed <= 0 then
		display.setNodesMiddle(self._goldPanel, nil, self._lvTitle, self._lvNeeded)
	elseif not lv and goldNeed > 0 then
		display.setNodesMiddle(self._goldPanel, nil, self._goldTitle, self._gold_icon, self._goldNeeded)
	end
end

function NewBagUpView:_onTipClose(tp)
	if tp == Enums.TIPS_HIDE.GOTO then
		executeFunc(self._onCloseView, self)
	end
end
-- function NewBagUpView:registerOnOperate( cb )
-- 	self._onPanelHandler = cb
-- end

return NewBagUpView
